$.namespace('sstjb');

sstjb = function() {
	var url = '',isLoaded = false;
	
	return {
		init : function() {
			sstjb.initGrid();
		},
		
		search : function() {
			var id = $('#cc').combobox('getValue');
			sstjb.initGrid(id);
		},
		count : function (id,obj) {
			var num = 0;
			for(var i = 0 ; i < obj.length ; i++){
				if(obj[i].regionID == id){
					num++;
				}
			}
			return num;
		},
		
		initGrid : function(id) {
			var c ;
			if(id!= null){
				c= sstjb.row(id);
			}
			$('#grid').datagrid({
				//title : '角色列表',
				url : ctx + '/rest/gcb/facility/findGroupByType',
				contentType : 'application/json;charset=utf-8',
				queryParams : {'gridID':id},
				fit : true,
				fitColumns : true,
				rownumbers : true,
				nowrap : true,
				striped : true,
				remoteSort : false,
				singleSelect : true,
				border : false,
				idField : 'id',
				columns : c,
				toolbar : '#toolbar'
			});
		},
		
		row : function(id){
			var array =[];
			var c = [];
			$.ajax({
				type : 'GET',
				url : ctx + '/rest/gcb/grid/findListByPid?id='+id,
				async:false,
				contentType : 'application/json;charset=UTF-8',
				success : function(result) {
					 array.push({field:'',title:'',width:'',formatter:''});
					 $(result).each(function(){
				          array.push({field:'',title:'',width:'',formatter:''});
				       });
					 array.push({field:'',title:'',width:'',formatter:''});
				       c.push(array);
				       var sum = [];
			    		   c[0][0]['field']= 'name';
				    	   c[0][0]['title']= '内容';
				    	   c[0][0]['width']= "100";
				    	   c[0][0]['formatter']= function(value,row,index){
				    		   sum[index] = 0;
				    		   for(var a in row){
				    			    if(row.hasOwnProperty(a)){
				    			        return a;
				    			    }
				    			}
				    	   }
				    	   
				       $(result).each(function(index,el){
				    	   c[0][index+1]['field']= el.id;
				    	   c[0][index+1]['title']= el.name;
				    	   c[0][index+1]['width']= "100";
				    	   c[0][index+1]['formatter']= function(value,row,index){
				    		   for(var prop in row){
				    			    if(row.hasOwnProperty(prop)){
				    			    	sum[index] = sum[index] + sstjb.count(el.id,row[prop]);
				    			        return sstjb.count(el.id,row[prop]);
				    			    }
				    			}
							}
				    	   
				      });
				       c[0][result.length+1]['field']= 'total';
			    	   c[0][result.length+1]['title']= '合计';
			    	   c[0][result.length+1]['width']= "100";
			    	   c[0][result.length+1]['formatter']= function(value,row,index){
			    		  return sum[index];
			    	   }
				}
			})
			return c;
		}

	}
		
}();
//sstjb.config = {
//		grid : {
//		id : "grid",
//		type : "easyui-datagrid",
//		option : {
//			url : ctx + '/rest/gcb/facility/findGroupByType',
//			contentType : 'application/json;charset=utf-8',
//			fit : true,
//			fitColumns : true,
//			rownumbers : false,
//			nowrap : true,
//			striped : true,
//			pagination : false,
//			remoteSort : false,
//			singleSelect : false,
//			border : false,
//			idField : 'id',
//			columns : columns,
//			toolbar : '#toolbar'
//		}
//	},
//}

// 初始化
$(function() {
	sstjb.init();
})



